<template>
  <view class="profile-container">
    <view class="user-card">
      <view class="user-avatar">
        <image :src="userInfo.avatar || '/static/avatar-default.png'" class="avatar" />
        <view class="admin-badge">管理员</view>
      </view>
      <view class="user-info">
        <text class="user-name">{{ userInfo.username }}</text>
        <text class="user-role">系统管理员</text>
        <text class="user-id">管理员ID: {{ userInfo.userId }}</text>
        <text class="user-department">信息技术中心</text>
      </view>
    </view>

    <view class="system-info">
      <view class="info-item">
        <text class="info-label">系统版本</text>
        <text class="info-value">v1.2.0</text>
      </view>
      <view class="info-item">
        <text class="info-label">最后维护</text>
        <text class="info-value">2024-01-15</text>
      </view>
      <view class="info-item">
        <text class="info-label">服务器状态</text>
        <text class="info-value status-online">正常运行</text>
      </view>
    </view>

    <view class="menu-section">
      <view class="section-title">系统管理</view>
      <view class="menu-list">
        <view class="menu-item" @click="navTo('dashboard')">
          <view class="menu-icon">📊</view>
          <text class="menu-text">数据看板</text>
          <text class="menu-arrow">›</text>
        </view>
        
        <view class="menu-item" @click="navTo('user-manage')">
          <view class="menu-icon">👥</view>
          <text class="menu-text">用户管理</text>
          <text class="menu-arrow">›</text>
        </view>
        
        <view class="menu-item" @click="navTo('classroom-manage')">
          <view class="menu-icon">🏫</view>
          <text class="menu-text">教室管理</text>
          <text class="menu-arrow">›</text>
        </view>
        
        <view class="menu-item" @click="navTo('log-manage')">
          <view class="menu-icon">📋</view>
          <text class="menu-text">系统日志</text>
          <text class="menu-arrow">›</text>
        </view>
      </view>
    </view>

    <view class="menu-section">
      <view class="section-title">系统设置</view>
      <view class="menu-list">
        <view class="menu-item" @click="navTo('system-settings')">
          <view class="menu-icon">⚙️</view>
          <text class="menu-text">基本设置</text>
          <text class="menu-arrow">›</text>
        </view>
        
        <view class="menu-item" @click="navTo('backup')">
          <view class="menu-icon">💾</view>
          <text class="menu-text">数据备份</text>
          <text class="menu-arrow">›</text>
        </view>
        
        <view class="menu-item" @click="navTo('permission')">
          <view class="menu-icon">🔐</view>
          <text class="menu-text">权限管理</text>
          <text class="menu-arrow">›</text>
        </view>
        
        <view class="menu-item" @click="showSystemUpdateDialog">
          <view class="menu-icon">🔄</view>
          <text class="menu-text">系统更新</text>
          <text class="menu-arrow">›</text>
        </view>
      </view>
    </view>

    <view class="menu-section">
      <view class="section-title">账户管理</view>
      <view class="menu-list">
        <view class="menu-item" @click="showEditDialog">
          <view class="menu-icon">👤</view>
          <text class="menu-text">个人信息</text>
          <text class="menu-arrow">›</text>
        </view>
        
        <view class="menu-item" @click="showPasswordDialog">
          <view class="menu-icon">🔒</view>
          <text class="menu-text">修改密码</text>
          <text class="menu-arrow">›</text>
        </view>
        
        <view class="menu-item" @click="handleLogout">
          <view class="menu-icon">🚪</view>
          <text class="menu-text">退出登录</text>
          <text class="menu-arrow">›</text>
        </view>
      </view>
    </view>

    <!-- 系统更新弹窗 -->
    <uni-popup ref="updatePopup" type="center">
      <view class="update-dialog">
        <text class="dialog-title">系统更新</text>
        <view class="update-info">
          <text class="current-version">当前版本: v1.2.0</text>
          <text class="latest-version">最新版本: v1.3.0</text>
        </view>
        <view class="update-features">
          <text class="features-title">更新内容:</text>
          <text class="feature-item">• 新增数据可视化功能</text>
          <text class="feature-item">• 优化系统性能</text>
          <text class="feature-item">• 修复已知问题</text>
        </view>
        <view class="dialog-actions">
          <button class="btn cancel" @click="$refs.updatePopup.close()">稍后更新</button>
          <button class="btn confirm" @click="startSystemUpdate">立即更新</button>
        </view>
      </view>
    </uni-popup>

    <!-- 修改信息弹窗 -->
    <uni-popup ref="editPopup" type="center">
      <view class="edit-dialog">
        <text class="dialog-title">修改个人信息</text>
        <view class="form-item">
          <text class="label">姓名</text>
          <input v-model="editForm.username" class="input" />
        </view>
        <view class="form-item">
          <text class="label">邮箱</text>
          <input v-model="editForm.email" class="input" />
        </view>
        <view class="form-item">
          <text class="label">电话</text>
          <input v-model="editForm.phone" class="input" />
        </view>
        <view class="form-item">
          <text class="label">部门</text>
          <input v-model="editForm.department" class="input" />
        </view>
        <view class="dialog-actions">
          <button class="btn cancel" @click="$refs.editPopup.close()">取消</button>
          <button class="btn confirm" @click="saveProfile">保存</button>
        </view>
      </view>
    </uni-popup>

    <!-- 修改密码弹窗 -->
    <uni-popup ref="passwordPopup" type="center">
      <view class="password-dialog">
        <text class="dialog-title">修改密码</text>
        <view class="form-item">
          <text class="label">原密码</text>
          <input v-model="passwordForm.oldPassword" password class="input" />
        </view>
        <view class="form-item">
          <text class="label">新密码</text>
          <input v-model="passwordForm.newPassword" password class="input" />
        </view>
        <view class="form-item">
          <text class="label">确认新密码</text>
          <input v-model="passwordForm.confirmPassword" password class="input" />
        </view>
        <view class="dialog-actions">
          <button class="btn cancel" @click="$refs.passwordPopup.close()">取消</button>
          <button class="btn confirm" @click="changePassword">确认</button>
        </view>
      </view>
    </uni-popup>
  </view>
</template>

<script>
export default {
  data() {
    return {
      userInfo: {},
      editForm: {
        username: '',
        email: '',
        phone: '',
        department: ''
      },
      passwordForm: {
        oldPassword: '',
        newPassword: '',
        confirmPassword: ''
      }
    }
  },
  
  onLoad() {
    this.loadUserInfo()
  },
  
  methods: {
    loadUserInfo() {
      this.userInfo = uni.getStorageSync('userInfo') || {
        username: '系统管理员',
        userId: 'admin001',
        avatar: '',
        email: 'admin@university.edu',
        phone: '13900139000',
        department: '信息技术中心'
      }
      this.editForm = { ...this.userInfo }
    },
    
    navTo(page) {
      const routes = {
        dashboard: '/pages/admin/dashboard/dashboard',
        'user-manage': '/pages/admin/user-manage/user-manage',
        'classroom-manage': '/pages/admin/classroom-manage/classroom-manage',
        'log-manage': '/pages/admin/log-manage/log-manage',
        'system-settings': '/pages/admin/system-settings/system-settings',
        'backup': '/pages/admin/backup/backup',
        'permission': '/pages/admin/permission/permission'
      }
      uni.navigateTo({ url: routes[page] })
    },
    
    showSystemUpdateDialog() {
      this.$refs.updatePopup.open()
    },
    
    showEditDialog() {
      this.$refs.editPopup.open()
    },
    
    showPasswordDialog() {
      this.passwordForm = {
        oldPassword: '',
        newPassword: '',
        confirmPassword: ''
      }
      this.$refs.passwordPopup.open()
    },
    
    startSystemUpdate() {
      uni.showLoading({ title: '更新中...' })
      
      // 模拟系统更新过程
      setTimeout(() => {
        uni.hideLoading()
        uni.showToast({ title: '系统更新成功', icon: 'success' })
        this.$refs.updatePopup.close()
      }, 2000)
    },
    
    saveProfile() {
      // 更新用户信息
      const updatedInfo = { ...this.userInfo, ...this.editForm }
      uni.setStorageSync('userInfo', updatedInfo)
      this.userInfo = updatedInfo
      
      uni.showToast({ title: '修改成功', icon: 'success' })
      this.$refs.editPopup.close()
    },
    
    changePassword() {
      if (!this.passwordForm.oldPassword || !this.passwordForm.newPassword) {
        uni.showToast({ title: '请填写完整信息', icon: 'none' })
        return
      }
      
      if (this.passwordForm.newPassword !== this.passwordForm.confirmPassword) {
        uni.showToast({ title: '两次密码输入不一致', icon: 'none' })
        return
      }
      
      // 这里应该调用修改密码的API
      uni.showToast({ title: '密码修改成功', icon: 'success' })
      this.$refs.passwordPopup.close()
    },
    
    handleLogout() {
      uni.showModal({
        title: '提示',
        content: '确定要退出登录吗？',
        success: (res) => {
          if (res.confirm) {
            // 清除登录信息
            uni.removeStorageSync('token')
            uni.removeStorageSync('userInfo')
            
            // 跳转到登录页
            uni.reLaunch({ url: '/pages/login/login' })
          }
        }
      })
    }
  }
}
</script>

<style scoped>
.profile-container {
  min-height: 100vh;
  background: #f5f5f5;
}

.user-card {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 60rpx 40rpx;
  color: white;
  display: flex;
  align-items: flex-start;
  position: relative;
}

.user-avatar {
  position: relative;
  margin-right: 30rpx;
}

.avatar {
  width: 120rpx;
  height: 120rpx;
  border-radius: 50%;
  border: 4rpx solid rgba(255, 255, 255, 0.3);
}

.admin-badge {
  position: absolute;
  top: -10rpx;
  right: -10rpx;
  background: #ff4757;
  color: white;
  padding: 6rpx 12rpx;
  border-radius: 20rpx;
  font-size: 20rpx;
  font-weight: bold;
}

.user-info {
  flex: 1;
}

.user-name {
  display: block;
  font-size: 36rpx;
  font-weight: bold;
  margin-bottom: 10rpx;
}

.user-role {
  display: block;
  font-size: 24rpx;
  opacity: 0.9;
  margin-bottom: 5rpx;
}

.user-id, .user-department {
  display: block;
  font-size: 22rpx;
  opacity: 0.8;
  margin-bottom: 5rpx;
}

.system-info {
  background: white;
  margin: 20rpx;
  border-radius: 15rpx;
  padding: 30rpx;
}

.info-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20rpx 0;
  border-bottom: 1rpx solid #f0f0f0;
}

.info-item:last-child {
  border-bottom: none;
}

.info-label {
  font-size: 26rpx;
  color: #666;
}

.info-value {
  font-size: 26rpx;
  color: #333;
  font-weight: 500;
}

.status-online {
  color: #4caf50;
}

.menu-section {
  background: white;
  margin: 20rpx;
  border-radius: 15rpx;
  overflow: hidden;
}

.section-title {
  font-size: 28rpx;
  font-weight: bold;
  color: #333;
  padding: 30rpx;
  border-bottom: 1rpx solid #f0f0f0;
}

.menu-list {
  padding: 0;
}

.menu-item {
  display: flex;
  align-items: center;
  padding: 30rpx;
  border-bottom: 1rpx solid #f0f0f0;
}

.menu-item:last-child {
  border-bottom: none;
}

.menu-icon {
  font-size: 40rpx;
  margin-right: 30rpx;
  width: 60rpx;
  text-align: center;
}

.menu-text {
  flex: 1;
  font-size: 28rpx;
  color: #333;
}

.menu-arrow {
  font-size: 36rpx;
  color: #999;
}

.update-dialog,
.edit-dialog,
.password-dialog {
  background: white;
  border-radius: 20rpx;
  padding: 40rpx;
  width: 600rpx;
}

.dialog-title {
  display: block;
  font-size: 36rpx;
  font-weight: bold;
  text-align: center;
  margin-bottom: 30rpx;
}

.update-info {
  margin-bottom: 30rpx;
}

.current-version, .latest-version {
  display: block;
  font-size: 24rpx;
  color: #666;
  margin-bottom: 10rpx;
}

.latest-version {
  color: #667eea;
  font-weight: bold;
}

.update-features {
  margin-bottom: 30rpx;
}

.features-title {
  display: block;
  font-size: 26rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 15rpx;
}

.feature-item {
  display: block;
  font-size: 24rpx;
  color: #666;
  margin-bottom: 8rpx;
}

.form-item {
  margin-bottom: 30rpx;
}

.label {
  display: block;
  font-size: 28rpx;
  color: #333;
  margin-bottom: 15rpx;
  font-weight: 500;
}

.input {
  border: 2rpx solid #e0e0e0;
  border-radius: 8rpx;
  padding: 20rpx;
  font-size: 28rpx;
  width: 100%;
  box-sizing: border-box;
}

.dialog-actions {
  display: flex;
  gap: 20rpx;
  margin-top: 40rpx;
}

.btn {
  flex: 1;
  border: none;
  border-radius: 8rpx;
  padding: 20rpx;
  font-size: 28rpx;
}

.cancel {
  background: #f5f5f5;
  color: #666;
}

.confirm {
  background: #667eea;
  color: white;
}
</style>